package com.jd.order.service.impl;

import com.jd.basic.util.LoginContext;
import com.jd.basic.util.PageList;
import com.jd.order.domain.OrderPetAcquisition;
import com.jd.order.dto.OrderDto;
import com.jd.order.mapper.OrderPetAcquisitionMapper;
import com.jd.order.query.OrderPetAcquisitionQuery;
import com.jd.order.service.IOrderPetAcquisitionService;
import com.jd.basic.service.impl.BaseServiceImpl;
import com.jd.org.domain.Employee;
import com.jd.org.domain.Shop;
import com.jd.org.mapper.EmployeeMapper;
import com.jd.org.mapper.ShopMapper;
import com.jd.pet.domain.Pet;
import com.jd.pet.domain.SearchMasterMsg;
import com.jd.pet.mapper.PetMapper;
import com.jd.pet.mapper.SearchMasterMsgAuditLogMapper;
import com.jd.pet.mapper.SearchMasterMsgMapper;
import com.jd.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.jaxb.SpringDataJaxb;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * 业务实现类：
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private PetMapper petMapper;

    @Override
    public PageList<OrderPetAcquisition> queryPage(OrderPetAcquisitionQuery query, HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLoginUser(request);
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        //判断当前登录员工是哪种角色
        Long shopId = employee.getShop_id();
        if (shopId != null) {
            Shop shop = shopMapper.loadById(shopId);
            //判断是否为店长
            if (shop.getAdmin_id() != employee.getId()) {
                query.setEmpId(employee.getId());
            }
            query.setShopId(shopId);
        }
        //平台管理员
        return super.queryPage(query);
    }

    @Override
    public void confirm(OrderDto orderDto) {
        Long id = orderDto.getId();
        BigDecimal money = orderDto.getMoney();
        Integer payType = orderDto.getPayType();
        //1.修改订单：state = 1，money，payType
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.loadById(id);
        orderPetAcquisition.setState(1);
        orderPetAcquisition.setPrice(money);
        orderPetAcquisition.setPaytype(payType);
        orderPetAcquisitionMapper.update(orderPetAcquisition);
        //2.修改寻主消息：state=2，note
        SearchMasterMsg msg = searchMasterMsgMapper.loadById(orderPetAcquisition.getSearchMasterMsgId());
        msg.setState(2);
        msg.setNote("交易成功");
        searchMasterMsgMapper.update(msg);


        //宠物
        Long petId = orderPetAcquisition.getPetId();
        Pet pet = petMapper.loadById(petId);
        //修改一个成本价 = money
        pet.setCostprice(money.toString());
        petMapper.update(pet);
        //@TODO - 支付模块

    }

    @Override
    public void remove(Long id) {
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadById(id);
        //将寻主消息中的shop_id置空
        searchMasterMsgMapper.reject(order.getSearchMasterMsgId());
        //删除宠物 - 没有成功收购
        petMapper.remove(order.getPetId());
        //删除订单
        super.remove(id);
    }
}
